﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SPXsltWs.SPXsltWsWebpart;
using tomvangaever.library.WS;
using tomvangaever.library.XSLT;

namespace SPxsltWs.Test
{
    [TestClass]
    public class SPXsltWsWebpartUserControlTest
    {
        [TestMethod]
        public void GetListCollectionFromTucRailTest()
        {
            var SoapAction = "";
            string Xslt = @"<?xml version='1.0' ?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:wss='http://schemas.microsoft.com/sharepoint/soap/'
version='1.0'>
  <xsl:output method='xml' indent='yes' />
  <xsl:template match='wss:List'>
    <div>
      <h3>
        Title:<xsl:value-of select='@Title' />
      </h3>
      <p>
        ID:<xsl:value-of select='@ID' />
      </p>
      <p>
        Description:<xsl:value-of select='@Description' />
      </p>
      <p>
        ImageUrl:
        <img>
          <xsl:attribute name='src'>
            <xsl:value-of select='@ImageUrl' />
          </xsl:attribute>
        </img>
        <xsl:value-of select='@ImageUrl' />
      </p>
      <p>
        Name:<xsl:value-of select='@Name' />
      </p>
      <p>
        BaseType:<xsl:value-of select='@BaseType' />
      </p>
      <p>
        ServerTemplate:
        <xsl:value-of select='@ServerTemplate' />
      </p>
      <p>
        Created:<xsl:value-of select='@Created' />
      </p>
      <p>
        Modified:<xsl:value-of select='@Modified' />
      </p>
      <p>
        LastDeleted:<xsl:value-of select='@LastDeleted' />
      </p>
      <p>
        Version:<xsl:value-of select='@Version' />
      </p>
      <p>
        Direction:<xsl:value-of select='@Direction' />
      </p>
      <p>
        ThumbnailSize:<xsl:value-of select='@ThumbnailSize' />
      </p>
      <p>
        WebImageWidth:<xsl:value-of select='@WebImageWidth' />
      </p>
      <p>
        WebImageHeight:<xsl:value-of select='@WebImageHeight' />
      </p>
      <p>
        Flags:<xsl:value-of select='@Flags' />
      </p>
      <p>
        ItemCount:<xsl:value-of select='@ItemCount' />
      </p>
      <p>
        AnonymousPermMask:<xsl:value-of select='@AnonymousPermMask' />
      </p>
      <p>
        Rootfolder:<xsl:value-of select='@RootFolder' />
      </p>
      <p>
        ReadSecurity:<xsl:value-of select='@ReadSecurity' />
      </p>
      <p>
        WriteSecurity:<xsl:value-of select='@WriteSecurity' />
      </p>
      <p>
        Author:<xsl:value-of select='@Author' />
      </p>
      <p>
        EventSinkAssembly:<xsl:value-of select='@EventSinkAssembly' />
      </p>
      <p>
        EventSinkClass:<xsl:value-of select='@EventSinkClass' />
      </p>
      <p>
        EventSinkData:<xsl:value-of select='@EventSinkData' />
      </p>
      <p>
        EmailInsertsFolder:<xsl:value-of select='@EmailInsertsFolder' />
      </p>
      <p>
        AllowDeletion:<xsl:value-of select='@AllowDeletion' />
      </p>
      <p>
        AllowMultiResponses:<xsl:value-of select='@AllowMultiResponses' />
      </p>
      <p>
        EnableAttachments:<xsl:value-of select='@EnableAttachments' />
      </p>
      <p>
        EnableModeration:<xsl:value-of select='@EnableModeration' />
      </p>
      <p>
        EnableVersioning:<xsl:value-of select='@EnableVersioning' />
      </p>
      <p>
        EnableVersioning:<xsl:value-of select='@Hidden' />
      </p>
      <p>
        MultipleDataList:<xsl:value-of select='@MultipleDataList' />
      </p>
      <p>
        Ordered:<xsl:value-of select='@Ordered' />
      </p>
      <p>
        ShowUser:<xsl:value-of select='@ShowUser' />
      </p>
    </div>
  </xsl:template>
</xsl:stylesheet>
";
            string WebServiceUrl = "http://www.tucrail.be/_vti_bin/lists.asmx";
            string SoapBody = @"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
                                    <soapenv:Body>
                                        <GetListCollection xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
                                        </GetListCollection>
                                    </soapenv:Body>
                                </soapenv:Envelope>";

            string xml = WSHelper.Invoke(WebServiceUrl, SoapAction, SoapBody);
            string data = XsltHelper.ApplyXSLTransformation(xml, Xslt);

        }

        [TestMethod]
        public void GetListItemsFromTucRailTest()
        {
            var SoapAction = "";
            string WebServiceUrl = "http://www.tucrail.be/_vti_bin/lists.asmx";
            string SoapBody = string.Format(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
                            <soapenv:Body>    
                                <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>      
                                    <listName>Documents</listName>      
                                    <viewFields>
                                        <ViewFields />
                                    </viewFields>      
                                    <query>        
                                        <Query />      
                                    </query>      
                                    <queryOptions>        
                                        <QueryOptions />          
                                    </queryOptions>     
                                </GetListItems>   
                            </soapenv:Body>   
                            </soapenv:Envelope>
            ");
            string Xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'
exclude-result-prefixes='s dt rs z'>
  <xsl:output method='html' encoding='utf-8' omit-xml-declaration='yes'/>
  <xsl:template match='z:row'>
    OK
  </xsl:template>
</xsl:stylesheet>";
            string xml = WSHelper.Invoke(WebServiceUrl, SoapAction, SoapBody);
            string data = XsltHelper.ApplyXSLTransformation(xml, Xslt);
        }

        [TestMethod]
        public void GetListCollectionFromFerrariTest()
        {
            var SoapAction = "";
            string Xslt = @"<?xml version='1.0' ?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:wss='http://schemas.microsoft.com/sharepoint/soap/'
version='1.0'>
  <xsl:output method='xml' indent='yes' />
  <xsl:template match='wss:List'>
    <div>
      <h3>
        Title:<xsl:value-of select='@Title' />
      </h3>
      <p>
        ID:<xsl:value-of select='@ID' />
      </p>
      <p>
        Description:<xsl:value-of select='@Description' />
      </p>
      <p>
        ImageUrl:
        <img>
          <xsl:attribute name='src'>
            <xsl:value-of select='@ImageUrl' />
          </xsl:attribute>
        </img>
        <xsl:value-of select='@ImageUrl' />
      </p>
      <p>
        Name:<xsl:value-of select='@Name' />
      </p>
      <p>
        BaseType:<xsl:value-of select='@BaseType' />
      </p>
      <p>
        ServerTemplate:
        <xsl:value-of select='@ServerTemplate' />
      </p>
      <p>
        Created:<xsl:value-of select='@Created' />
      </p>
      <p>
        Modified:<xsl:value-of select='@Modified' />
      </p>
      <p>
        LastDeleted:<xsl:value-of select='@LastDeleted' />
      </p>
      <p>
        Version:<xsl:value-of select='@Version' />
      </p>
      <p>
        Direction:<xsl:value-of select='@Direction' />
      </p>
      <p>
        ThumbnailSize:<xsl:value-of select='@ThumbnailSize' />
      </p>
      <p>
        WebImageWidth:<xsl:value-of select='@WebImageWidth' />
      </p>
      <p>
        WebImageHeight:<xsl:value-of select='@WebImageHeight' />
      </p>
      <p>
        Flags:<xsl:value-of select='@Flags' />
      </p>
      <p>
        ItemCount:<xsl:value-of select='@ItemCount' />
      </p>
      <p>
        AnonymousPermMask:<xsl:value-of select='@AnonymousPermMask' />
      </p>
      <p>
        Rootfolder:<xsl:value-of select='@RootFolder' />
      </p>
      <p>
        ReadSecurity:<xsl:value-of select='@ReadSecurity' />
      </p>
      <p>
        WriteSecurity:<xsl:value-of select='@WriteSecurity' />
      </p>
      <p>
        Author:<xsl:value-of select='@Author' />
      </p>
      <p>
        EventSinkAssembly:<xsl:value-of select='@EventSinkAssembly' />
      </p>
      <p>
        EventSinkClass:<xsl:value-of select='@EventSinkClass' />
      </p>
      <p>
        EventSinkData:<xsl:value-of select='@EventSinkData' />
      </p>
      <p>
        EmailInsertsFolder:<xsl:value-of select='@EmailInsertsFolder' />
      </p>
      <p>
        AllowDeletion:<xsl:value-of select='@AllowDeletion' />
      </p>
      <p>
        AllowMultiResponses:<xsl:value-of select='@AllowMultiResponses' />
      </p>
      <p>
        EnableAttachments:<xsl:value-of select='@EnableAttachments' />
      </p>
      <p>
        EnableModeration:<xsl:value-of select='@EnableModeration' />
      </p>
      <p>
        EnableVersioning:<xsl:value-of select='@EnableVersioning' />
      </p>
      <p>
        EnableVersioning:<xsl:value-of select='@Hidden' />
      </p>
      <p>
        MultipleDataList:<xsl:value-of select='@MultipleDataList' />
      </p>
      <p>
        Ordered:<xsl:value-of select='@Ordered' />
      </p>
      <p>
        ShowUser:<xsl:value-of select='@ShowUser' />
      </p>
    </div>
  </xsl:template>
</xsl:stylesheet>
";
            string WebServiceUrl = "http://www.ferrari.com/_vti_bin/lists.asmx";
            string SoapBody = @"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
                                    <soapenv:Body>
                                        <GetListCollection xmlns='http://schemas.microsoft.com/sharepoint/soap/'>
                                        </GetListCollection>
                                    </soapenv:Body>
                                </soapenv:Envelope>";

            string xml = WSHelper.Invoke(WebServiceUrl, SoapAction, SoapBody);
            string data = XsltHelper.ApplyXSLTransformation(xml, Xslt);

        }

        [TestMethod]
        public void GetListItemsFromFerrariTest()
        {
            var SoapAction = "";
            string WebServiceUrl = "http://www.ferrari.com/_vti_bin/lists.asmx";
            string SoapBody = string.Format(@"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>
                            <soapenv:Body>    
                                <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>      
                                    <listName>Style Library</listName>      
                                    <viewFields>
                                        <ViewFields />
                                    </viewFields>      
                                    <query>        
                                        <Query />      
                                    </query>      
                                    <queryOptions>        
                                        <QueryOptions />          
                                    </queryOptions>     
                                </GetListItems>   
                            </soapenv:Body>   
                            </soapenv:Envelope>
            ");
            string Xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'
exclude-result-prefixes='s dt rs z'>
  <xsl:output method='html' encoding='utf-8' omit-xml-declaration='yes'/>
  <xsl:template match='z:row'>
    OK
  </xsl:template>
</xsl:stylesheet>";
            string xml = WSHelper.Invoke(WebServiceUrl, SoapAction, SoapBody);
            string data = XsltHelper.ApplyXSLTransformation(xml, Xslt);
        }

        [TestMethod]
        public void GetGoogleWeatherResultTest()
        {
            try
            {
                var SoapAction = string.Empty;
                string SoapBody = string.Empty;
                string Xslt = @"<?xml version='1.0' ?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
	<xsl:output indent='yes' method='html' encoding='us-ascii'/>
	<xsl:template match='/'>
		<!--STYLE-->
		<style type='text/css'>
			.tvg-weather-body
		    {
		        position: relative;
		        clear: both;
		        width: 250px;
		    }  
		    .tvg-weather-image
		    {
		        position: relative;
		        float: left;
		        padding: 7px;
				width: 50px;
		    }    
		    .tvg-weather-details
		    {
		        padding: 7px;
		        position: relative;
		        float: right;
				text-align:left;
				width: 150px;
		    }  
			.tvg-weather-title{
				color:#21374C;
			}
    	</style>
		<!--HTML-->
		<!-- TODAY -->
		<div>
			<b class='tvg-weather-title'>Current</b>
		</div>
		<div class='tvg-weather-body'>
			<div class='tvg-weather-image'>
				<img>
					<xsl:attribute name='src'>http://www.google.com<xsl:value-of select='xml_api_reply/weather/current_conditions/icon/@data'/></xsl:attribute>
				</img>
			</div>
			<div class='tvg-weather-details'>				
				<div>
					<b>
						<xsl:value-of select='xml_api_reply/weather/current_conditions/temp_c/@data'/> °C
					</b>
				</div>
				<div>
					<xsl:value-of select='xml_api_reply/weather/current_conditions/condition/@data'/>
				</div>
			</div>
		</div>
		<!-- FORECAST -->
		<div style='clear:both;'>
		<b class='tvg-weather-title'>Forecasts</b>
		</div>
		<xsl:for-each select='xml_api_reply/weather/forecast_conditions'>
			<div class='tvg-weather-body'>
				<div class='tvg-weather-image'>
					<img>
						<xsl:attribute name='src'>http://www.google.com<xsl:value-of select='icon/@data'/></xsl:attribute>
					</img>
				</div>
				<div class='tvg-weather-details'>
					<div>
						<b>
							<i>
								<xsl:value-of select='day_of_week/@data'/>
							</i>
						</b>
					</div>
					<div>
						<b>min: <xsl:value-of select='floor((low/@data - 32) * (5 div 9))'/>°C - max: <xsl:value-of select='floor((high/@data - 32) * (5 div 9))'/>°C</b>
					</div>
					<div>
						<xsl:value-of select='condition/@data'/>
					</div>
				</div>
			</div>
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
";
                string WebServiceUrl = "http://www.google.com/ig/api?weather=Brussels";
                string xml = WSHelper.Invoke(WebServiceUrl, SoapAction, SoapBody);
                string data = XsltHelper.ApplyXSLTransformation(xml, Xslt);
            }
            catch (Exception ex) {
                Assert.Fail(ex.Message);
            }
        }
    }
}
